package com.sztle.admodule.handler;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationAdEcpmInfo;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager;
import com.sztle.admodule.GmAdManager;
import com.sztle.admodule.IShowListener;
import com.sztle.admodule.bean.AdConfigInfo;
import com.sztle.admodule.bean.RequestAdEntity;
import com.sztle.admodule.outstatus.AppStatusProcessor;
import com.sztle.admodule.control.manager.ControlManager;
import com.sztle.admodule.myutils.AdLogUtils;

public class RewardHandler {
    private static final String TAG = "RewardShower";
    private static TTRewardVideoAd useRewardVideoAd; // 插全屏广告对象
    private static boolean isLoading = false;

    private static String getCode(){
        AdConfigInfo adConfigInfo = ControlManager.getAdConfigInfo();
        if(adConfigInfo != null && !TextUtils.isEmpty(adConfigInfo.rewardId.trim())){
            return adConfigInfo.rewardId;
        }
        return AppStatusProcessor.getAppParam().rewardedVideoAdId;
    }

    public static void loadAndShow(Activity activity, IShowListener showListener){
        if (activity == null) {
            if (showListener != null) {
                showListener.onJustEnd(false);
            }
            return;
        }

        if(!GmAdManager.isUseAdExp()){
            if(showListener != null){
                showListener.onJustEnd(false);
            }
            return;
        }
        if(isLoading){
            if(showListener != null){
                showListener.onJustEnd(false);
            }
            return;
        }
        GmAdManager.enterRequestAdQueue(new RequestAdEntity(RequestAdEntity.TYPE_AD_REWARD));
        /** 1、创建AdSlot对象 */
        AdSlot adslot = new AdSlot.Builder()
                .setCodeId(getCode())
                .setOrientation(TTAdConstant.ORIENTATION_VERTICAL)
                .build();


        /** 2、创建TTAdNative对象 */
        TTAdNative adNativeLoader = GmAdManager.get().createAdNative(activity);

        /** 3、加载广告 */
        isLoading = true;
        adNativeLoader.loadRewardVideoAd(adslot,new TTAdNative.RewardVideoAdListener(){

            @Override
            public void onError(int i, String s) {
                isLoading = false;
                AdLogUtils.Log(TAG,"onError = "+s);
                if (showListener != null) {
                    showListener.onAdLoadEnd(false);
                }
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                isLoading = false;
                AdLogUtils.Log(TAG,"onRewardVideoAdLoad");
                useRewardVideoAd = ttRewardVideoAd;
                if (showListener != null) {
                    showListener.onAdLoadEnd(true);
                }
            }

            @Override
            public void onRewardVideoCached() {
                AdLogUtils.Log(TAG,"onRewardVideoCached");
            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                AdLogUtils.Log(TAG,"onRewardVideoCached 1");
                useRewardVideoAd = ttRewardVideoAd;
                show(activity,showListener);
            }
        });
    }

    private static void show(Activity activity,IShowListener showListener){
        if (activity == null || useRewardVideoAd == null){
            if (showListener != null) {
                showListener.onJustEnd(false);
            }
            AdLogUtils.Log(TAG,"RewardVideoAd is null");
            return;
        }
        if(useRewardVideoAd.getMediationManager() == null || !useRewardVideoAd.getMediationManager().isReady()){
            if (showListener != null) {
                showListener.onJustEnd(false);
            }
            AdLogUtils.Log(TAG,"ad not ready");
            return;
        }

        useRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                AdLogUtils.Log(TAG,"onAdShow");
                //showEcpmInfo();
                if(showListener != null){
                    showListener.onAdShow(getEcpm());
                }
            }

            @Override
            public void onAdVideoBarClick() {
                AdLogUtils.Log(TAG,"onAdVideoBarClick");
            }

            @Override
            public void onAdClose() {
                AdLogUtils.Log(TAG,"onAdClose");
                if(showListener != null){
                    showListener.onJustEnd(true);
                }
            }

            @Override
            public void onVideoComplete() {
                AdLogUtils.Log(TAG,"onVideoComplete");
            }

            @Override
            public void onVideoError() {
                AdLogUtils.Log(TAG,"onVideoError");
                if(showListener != null){
                    showListener.onJustEnd(false);
                }
            }

            @Override
            public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {
                AdLogUtils.Log(TAG,"onRewardVerify");
            }

            @Override
            public void onRewardArrived(boolean b, int i, Bundle bundle) {
                AdLogUtils.Log(TAG,"onRewardArrived");
                if(showListener != null){
                    showListener.onAdReward();
                }
            }

            @Override
            public void onSkippedVideo() {
                AdLogUtils.Log(TAG,"onSkippedVideo");
            }
        });

        useRewardVideoAd.showRewardVideoAd(activity);
    }


    private static String getEcpm() {
        if (useRewardVideoAd == null) {
            return "";
        }
        MediationBaseManager mediationManager = useRewardVideoAd.getMediationManager();
        if (mediationManager != null) {
            MediationAdEcpmInfo showEcpm = mediationManager.getShowEcpm();
            return showEcpm.getEcpm();
        }
        return "";
    }

    private static void showEcpmInfo() {
        if (useRewardVideoAd == null) {
            return;
        }
        MediationBaseManager mediationManager = useRewardVideoAd.getMediationManager();
        if (mediationManager != null) {
            MediationAdEcpmInfo showEcpm = mediationManager.getShowEcpm();
            if (showEcpm != null) {
               // logEcpmInfo(showEcpm);
            }
        }
    }

    private static void logEcpmInfo(MediationAdEcpmInfo item) {
        AdLogUtils.Log(TAG, "EcpmInfo: \n" +
                "SdkName: " + item.getSdkName() + ",\n" +
                "CustomSdkName: " + item.getCustomSdkName() + ",\n" +
                "SlotId: " + item.getSlotId() + ",\n" +
                // 单位：分
                "Ecpm: " + item.getEcpm() + ",\n" +
                "ReqBiddingType: " + item.getReqBiddingType() + ",\n" +
                "ErrorMsg: " + item.getErrorMsg() + ",\n" +
                "RequestId: " + item.getRequestId() + ",\n" +
                "RitType: " + item.getRitType() + ",\n" +
                "AbTestId: " + item.getAbTestId() + ",\n" +
                "ScenarioId: " + item.getScenarioId() + ",\n" +
                "SegmentId: " + item.getSegmentId() + ",\n" +
                "Channel: " + item.getChannel() + ",\n" +
                "SubChannel: " + item.getSubChannel() + ",\n" +
                "customData: " + item.getCustomData()
        );
    }
}
